﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Diagnostics;
using Newtonsoft.Json;

namespace Serializable
{
    class Program
    {
        string fileLoc = @"C:\Temp\Car.cr";

        static void Main(string[] args)
        {
            Program p = new Program();
            p.TestSave();
            p.TestLoad();
            Console.ReadLine();
        }

        public void TestSave()
        {
            Car car = new Car();
            car.Name = "Vibe";
            car.Year = "2004";
            car.Model = "GT";
            car.Driver = new People("ANDREW");
            for (int i = 0; i < 2; i++)
            {
                car.Passengers.Add(new People("Andrew"));
                car.Passengers.Add(new People("Christine"));
            }


            car.Save(fileLoc);
        }

        public void TestLoad()
        {
            Car car = Car.Load(fileLoc);
            Console.WriteLine(car.Name);
            Console.WriteLine(car.Model);
            Console.WriteLine(car.Year);
            Console.WriteLine(car.Driver.Name);
            foreach (var p in car.Passengers)
                Console.WriteLine(p.Name);
        }
    }

    [Serializable]
    public class People
    {
        public string Name { get; set; }

        public People(string name)
        {
            Name = name;
        }
    }

    [Serializable]
    public class Car
    {
        public string Name { get; set; }
        public string Year { get; set; }
        public string Model { get; set; }
        public People Driver { get; set; }
        public List<People> Passengers { get; set; }

        const int VERSION = 1;
        public Car()
        {
            Passengers = new List<People>();
        }

        public void Save(string fileName)
        {
            Stream stream = null;
            try
            {
                string car = JsonConvert.SerializeObject(this, Formatting.Indented);
                File.WriteAllText(fileName, car);
            }
            catch
            {
                // do nothing, just ignore any possible errors
            }
            finally
            {
                if (null != stream)
                    stream.Close();
            }

        }

        public static Car Load(string fileName)
        {
            Stream stream = null;
            Car car = null;
            try
            {
                string s = File.ReadAllText(fileName);
                car = JsonConvert.DeserializeObject<Car>(s);
            }
            catch
            {
                // do nothing, just ignore any possible errors
            }
            finally
            {
                if (null != stream)
                    stream.Close();
            }

            return car;
        } 
    }
}
